package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.entity.BConfig;
import com.cxf.windex.entity.BResouces;
import com.cxf.windex.entity.BShop;
import com.cxf.windex.mapper.BResoucesMapper;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.ApiModelProperty;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @program: business_needs
 * @description: 资源方Biz
 * @author: chenxf
 * @create: 2018-10-16 16:37
 **/
@Service
public class BResourcesBiz extends BaseBiz<BResoucesMapper,BResouces> {
  @Autowired
  private BConfigBiz configBiz;
  @Autowired
  private MoneyBiz moneyBiz;

  public List<BResouces> list(int limit, int offset,  String orderStr,String name,String provice,String city,String county,String status,String type) {
    return list(limit,offset,orderStr,name,provice,city,county,status,type,"");
  }
    /**
     * 资源列表
     *
     * @param limit 数量
     * @param offset 页码
     * @param orderStr  排序(升序或者降序)
     * @param name 资源名称
     * @return
     */
  public List<BResouces> list(int limit, int offset,  String orderStr,String name,String provice,String city,String county,String status,String type,String town) {

    Example example = new Example(BResouces.class);
    Example.Criteria criteria = example.createCriteria();
    if (StringUtils.isNotBlank(name)) {
      criteria.andLike("name", "%" + name + "%");
    }
    if (StringUtils.isNotBlank(type)) {
      criteria.andEqualTo("type", type);
    }
    if (StringUtils.isNotBlank(status)) {
      criteria.andEqualTo("status",  status );
    }
    if (StringUtils.isNotBlank(provice)) {
      criteria.andEqualTo("provice", provice);
    }
    if (StringUtils.isNotBlank(town)) {
      criteria.andEqualTo("town", town);
    }

    if (StringUtils.isNotBlank(city)) {
      criteria.andEqualTo("city",   city  );
    }
    if (StringUtils.isNotBlank(county)) {
      criteria.andEqualTo("county",  county  );
    }
    example.setOrderByClause("crt_time " + orderStr);
    PageHelper.startPage(offset, limit);
    return selectByExample(example);
  }

  /**
   * 根据名称查找对应有效资源
   * @param name
   * @return
   */
  public List<BResouces> adminList(String name){
    Example example = new Example(BResouces.class);
    Example.Criteria criteria = example.createCriteria();
    if (StringUtils.isNotBlank(name)) {
      criteria.andLike("name", "%" + name + "%");
    }
    criteria.andIsNotNull("wxopenid");
    criteria.andEqualTo("status",  1 );
    example.setOrderByClause("crt_time desc" );
    return selectByExample(example);
  }

  /**
   * 根据IDS获取资源列表
   * @param ids 资源IDS逗号分隔
   * @return
   */
  public List<BResouces> idsList(String ids) {
    if(StringUtils.isBlank(ids)){
      return new ArrayList<BResouces>();
    }
    Example example = new Example(BResouces.class);
    Example.Criteria criteria = example.createCriteria();
    if (StringUtils.isNotBlank(ids)) {
      Iterable<String> idsIterables = Arrays.asList(ids.split(","));
      criteria.andIn("id",idsIterables);
    }
    example.setOrderByClause("crt_time asc" );
    return selectByExample(example);
  }
  /**
   * 获取资源信息
   * @param userId
   * @return
   */
  public BResouces getInfo(String userId) {
    BResouces entity=new BResouces();
    entity.setWxopenid(userId);
    return selectOne(entity);
  }

  /**
   * 保存资源
   * @param entity
   */
  public void save(BResouces entity){
   if(entity.getId()==null||entity.getId()==0){
       insertSelective(entity);
       moneyBiz.initMoney(entity.getWxopenid(),entity.getName());
    }else if("0".equals(entity.getStatus())) {
     updateSelectiveById(entity);
   } else {
        BConfig bConfig=configBiz.getInfo(UserConstant.CONFIG_LW);
       if(entity.getLPrice()!=null && entity.getLPrice().intValue()>0 ){
         entity.setPayLPrice(entity.getLPrice().multiply(BigDecimal.valueOf(100)).divide(bConfig.getLF(),0,BigDecimal.ROUND_DOWN));
       }
       if(entity.getVPrice()!=null && entity.getVPrice().intValue()>0){
         entity.setPayVPrice(entity.getVPrice().multiply(BigDecimal.valueOf(100)).divide(bConfig.getVF(),0,BigDecimal.ROUND_DOWN));
       }
       updateSelectiveById(entity);
    }
  }
}
